﻿using B.S.BaseData.Domain;
using BaseData.ReadApi.Appcontext.Command.Descendant_Files;
using BaseData.ReadApi.Appcontext.Command.Ewe_Files;
using BaseData.ReadApi.Appcontext.Command.EweHistoryRecord_Files;
using BaseData.ReadApi.Appcontext.Command.Ram_Files;
using BaseData.ReadApi.Appcontext.Command.Sheep_Records;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartTracking.API.Read.Application.Commands;
using SmartTracking.API.Read.Application.Commands.Descendant_Files;
using SmartTracking.API.Read.Application.Commands.Ewe_Files;
using SmartTracking.API.Read.Application.Commands.EWEQuery;
using SmartTracking.API.Read.Application.Commands.Initialform_Files;
using SmartTracking.API.Read.Application.Commands.Permission_command;
using SmartTracking.API.Read.Application.Commands.PoultryHouse_command;
using SmartTracking.API.Read.Application.Commands.Ram_Files;
using SmartTracking.API.Read.Application.Commands.Role_command;
using SmartTracking.API.Read.Application.Commands.SheepFarm_command;
using SmartTracking.API.Read.Application.Commands.User_command;
using SmartTracking.API.Read.Application.Commands.ZXL.Deathr;
using SmartTracking.API.Read.Application.Commands.ZXL.Eliminater;
using SmartTracking.API.Read.Application.Commands.ZXL.Procurementr;
using SmartTracking.API.Read.Application.Commands.ZXL.Salesr;
using SmartTracking.API.Read.DTO;
using SmartTracking.API.Read.EweDTO;
using SmartTracking.ApiRead.Applications.Commands;
using SmartTracking.Domain.EWE;
using SmartTracking.Domain.HQX;
using SmartTracking.Domain.Sheep;
using SmartTracking.Domain.UserInfo;
using SmartTracking.ErrorCode;

namespace SmartTracking.API.Read.Controllers
{
    /// <summary>
    /// API-控制器-读取
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    [AllowAnonymous]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ManagementController> logger;

        public ManagementController(IMediator mediator,ILogger<ManagementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回登录任务</returns>
        [HttpPost]
        [AllowAnonymous]
        public Task<ApiResult<UserRoleDto>> Login(LoginCommand request)
        {
            try
            {
                logger.LogInformation("登录成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("登录失败" + ex.Message);
                throw;
            }
        }
        #region 用户管理
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回用户列表</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<UserDto>>> GetUser([FromQuery]GetUserCommand request)
        {
            try
            {
                logger.LogInformation("获取用户列表成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("获取用户列表失败"+ex.Message);
                throw;
            }
        }
        #endregion

        #region 角色管理
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回角色列表</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<RolePermissionDto>>> GetRole([FromQuery]GetRoleCommand request)
        {
            try
            {
                logger.LogInformation("获取角色列表成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("获取角色列表失败" + ex.Message);
                throw;
            }
        }
        #endregion

        #region 权限管理
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回权限列表</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<Permissions>>> GetPermission([FromQuery]GetPermissionCommand request)
        {
            try
            {
                logger.LogInformation("获取权限列表成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("获取权限列表失败" + ex.Message);
                throw;
            }
        }
        #endregion

        #region 羊只管理
        /// <summary>
        /// 获取羊场列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回羊场列表任务</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<SheepFarm>>> GetSheepFarm([FromQuery]GetSheepFarmCommand request)
        {
            try
            {
                logger.LogInformation("获取羊场列表成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("获取羊场列表失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取栋舍列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回栋舍列表任务</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<PoultryHouse>>> GetPoultryHouse([FromQuery]GetPoultryHouseCommand request)
        {
            try
            {
                logger.LogInformation("获取栋舍列表成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("获取栋舍列表失败" + ex.Message);
                throw;
            }
        }
        #endregion

        #region 母羊配种管理
        /// <summary>
        /// 母羊配种列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<Ewematings>>> GetEwematings([FromQuery] GetEwematingsQueryCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 妊检列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<MainPregnancyDTO>>> GetPregnancy([FromQuery] GetPregnancyCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 分娩列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<MainEweDeliveryDTO>>> GetDelivery([FromQuery] GetDeliveryCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 断奶列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<MainWeaningDTO>>> GetWeanings([FromQuery] GetWeaningsCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 反填母羊配种
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Ewematings>> GetEwematingsId([FromQuery] GetEwematingsIdCommand request)
        {
            return await mediator.Send(request);
        }
        #endregion


        #region 羊只销售管理

        /// <summary>
        /// 查询死亡列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<Death>> GetDeath([FromQuery]GetDeathCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 查询淘汰列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<Eliminate>> GetEliminate([FromQuery]GetEliminateCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 查询采购列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<Procurement>> GetProcurement([FromQuery]GetProcurementCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 查询销售列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<Sales>> GetSales([FromQuery]GetSalesCommand request)
        {
            return mediator.Send(request);
        }
        
        #endregion


        #region 羊只档案管理
        /// <summary>
        /// 查询羊只档案
        /// </summary>
        /// <param name="cmd">查询羊只档案请求</param>
        /// <returns>处理结果</returns>
        [HttpGet]
        public async Task<ApiResult<SheepRecords>> GetSheepRecordById([FromQuery] GetSheepRecordByIdCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        
        /// <summary>
        /// 查询后裔档案
        /// </summary>
        /// <param name="cmd">查询后裔档案请求</param>
        /// <returns>处理结果</returns>
        [HttpGet]
        public async Task<ApiResult<DescendantFile>> GetDescendantFileById([FromQuery] GetDescendantFileByIdCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 查询母羊档案
        /// </summary>
        /// <param name="cmd">查询母羊档案请求</param>
        /// <returns>处理结果</returns>
        [HttpGet]
        public async Task<ApiResult<EweFile>> GetEweFileById([FromQuery] GetEweFileByIdCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 查询种公档案
        /// </summary>
        /// <param name="cmd">查询种公档案请求</param>
        /// <returns>处理结果</returns>
        [HttpGet]
        public async Task<ApiResult<RamFile>> GetRamFileById([FromQuery] GetRamFileByIdCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 分页查询后裔档案
        /// </summary>
        /// <param name="cmd">分页查询参数</param>
        /// <returns>分页查询结果</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<DescendantFile>>> GetDescendantFilesPaged([FromQuery] GetDescendantFilesPagedCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 分页查询母羊档案
        /// </summary>
        /// <param name="cmd">分页查询参数</param>
        /// <returns>分页查询结果</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<EweFile>>> GetEweFilesPaged([FromQuery] GetEweFilesPagedCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 分页查询种公羊档案
        /// </summary>
        /// <param name="cmd">分页查询参数</param>
        /// <returns>分页查询结果</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<RamFile>>> GetRamFilesPaged([FromQuery] GetRamFilesPagedCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 分页查询羊只记录
        /// </summary>
        /// <param name="cmd">分页查询参数</param>
        /// <returns>分页查询结果</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<SheepRecords>>> GetSheepRecordsPaged([FromQuery] GetSheepRecordsPagedCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 获取后裔档案下拉框菜单
        /// </summary>
        /// <returns>下拉框菜单项列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<ResMenuitemOutput>>> GetDescendantFileMenu()
        {
            var cmd = new GetDescendantFileMenuCommand();
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 获取母羊档案下拉框菜单
        /// </summary>
        /// <returns>下拉框菜单项列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<ResMenuitemOutput>>> GetEweFileMenu()
        {
            var cmd = new GetEweFileMenuCommand();
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 获取种公羊档案下拉框菜单
        /// </summary>
        /// <returns>下拉框菜单项列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<ResMenuitemOutput>>> GetRamFileMenu()
        {
            var cmd = new GetRamFileMenuCommand();
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 根据ID查询初期表单
        /// </summary>
        /// <param name="cmd">查询初期表单请求</param>
        /// <returns>处理结果</returns>
        [HttpGet]
        public async Task<ApiResult<Initialform>> GetInitialformById([FromQuery] GetInitialformByIdCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 分页查询初期表单
        /// </summary>
        /// <param name="cmd">分页查询参数</param>
        /// <returns>分页查询结果</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<Initialform>>> GetInitialformPaged([FromQuery] GetInitialformPagedCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 获取种母历史记录分页列表
        /// </summary>
        /// <param name="command">查询参数</param>
        /// <returns>分页结果</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<EweHistoryRecordDto>>> GetEweHistoryRecords([FromQuery] GetEweHistoryRecordListCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 获取种母历史记录详情
        /// </summary>
        /// <param name="command">查询参数</param>
        /// <returns>详情结果</returns>
        [HttpGet]
        public async Task<ApiResult<EweHistoryRecordDetailDto>> GetEweHistoryRecordDetail([FromQuery] GetEweHistoryRecordDetailCommand command)
        {
            return await mediator.Send(command);
        }
        #endregion
    }
}
